package com.cb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.common.R;
import com.cb.dto.SetmealDto;
import com.cb.entity.Category;
import com.cb.entity.Setmeal;
import com.cb.service.CategoryService;
import com.cb.service.SetmealDishService;
import com.cb.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐")
    @ApiImplicitParam(name = "setmealDto",value = "套餐信息",required = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("setmeal:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功！");
    }

    /**
     * 套餐信息分页查询
     */
    @GetMapping("/page")
    @ApiOperation(value = "套餐信息分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<IPage<SetmealDto>> page(int page, int pageSize, String name){
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        setmealService.getSetmeal(pageInfo,name);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list= records.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();

            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            //根据id查分类对象
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "删除套餐")
    @ApiImplicitParam(name = "ids",value = "套餐id集合",required = true)
    public R<String> deletes(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功！");
    }

    /**
     * 起售停售套餐
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "起售停售套餐")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",value = "状态",required = true),
            @ApiImplicitParam(name = "ids",value = "套餐id集合",required = true)
    })
    public R<String> sale(@PathVariable int status,String[] ids){
        setmealService.sale(status,ids);
        return R.success("修改成功！");
    }

    /**
     * 修改套餐
     */
    //根据Id查询套餐信息(数据回显)
    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id查询套餐信息")
    @ApiImplicitParam(name = "id",value = "套餐id",required = true)
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }

    //修改套餐信息
    @PutMapping
    @ApiOperation(value = "修改套餐信息")
    @ApiImplicitParam(name = "setmealDto",value = "套餐信息",required = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功！");
    }

    /**
     * 显示套餐信息
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @ApiOperation(value = "显示套餐信息")
//    @ApiImplicitParam(name = "setmeal",value = "套餐信息",required = true)
    public R<List<Setmeal>> list(Setmeal setmeal){
        List<Setmeal> setmealList=setmealService.getList(setmeal);
        return R.success(setmealList);
    }

    /**
     * 移动端套餐详情
     */
    @GetMapping("/dish/{id}")
    @ApiOperation(value = "移动端套餐详情")
    @ApiImplicitParam(name = "id",value = "套餐id",required = true)
    public R<SetmealDto> getDish(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }
}
